<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-json-lib.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->

<%
'数据要求格式：
'[{"goods_name":"文具名1","goods_stock":20,"goods_unitprice":20},{"goods_name":"文具名2","goods_stock":20,"goods_unitprice":20}]
Response.ContentType = "application/json"
dim json,rs,sql
set json=jsObject()
Set rs = Server.CreateObject("ADODB.Recordset")
action=request("action")
if action="add" then
	json("bStatus")=false
	conn.BeginTrans
	purchase_vendor=request("purchase_vendor")
	purchase_date=request("purchase_date")
	purchase_sn=request("purchase_sn")
	purchase_contact=request("purchase_contact")
	purchase_bill=request("purchase_bill")
	purchase_memo=request("purchase_memo")
	' purchase_price=request("purchase_price")
	purchase_operator=session("iId")
	purchase_update=now()
	
	if purchase_bill<>1 then purchase_bill=0
	' if purchase_price="" then purchase_price=0
	if purchase_sn="" then purchase_sn="空"
	sql="insert into purchase(purchase_vendor,purchase_date,purchase_sn,purchase_contact,purchase_bill,purchase_memo,purchase_operator) values("&purchase_vendor&",'"&purchase_date&"','"&purchase_sn&"','"&purchase_contact&"',"&purchase_bill&",'"&purchase_memo&"',"&purchase_operator&")"
	purchase_id=ExecuteCommand(sql,true)
	goods_number=request("goods_number")
	agoods_id=split(request("goods_id"),",")
	agoods_class=split(request("goods_class"),",")
	agoods_name=split(request("goods_name"),",")
	agoods_brand=split(request("goods_brand"),",")
	agoods_model=split(request("goods_model"),",")
	agoods_spec=split(request("goods_spec"),",")
	agoods_unit=split(request("goods_unit"),",")
	agoods_quantity=split(request("goods_quantity"),",")
	agoods_unitprice=split(request("goods_unitprice"),",")
	agoods_price=split(request("goods_price"),",")
	agoods_memo=split(request("goods_memo"),",")
	for i=0 to goods_number-1
		goods_id=trim(agoods_id(i))
		goods_class=trim(agoods_class(i))
		goods_name=trim(agoods_name(i))
		goods_brand=trim(agoods_brand(i))
		' 判断agoods_model是否是空数组，因为split一个空字符串会返回一个空数组
		if ubound(agoods_model)=-1 then goods_model="" else goods_model=trim(agoods_model(i))
		if ubound(agoods_spec)=-1 then goods_spec="" else goods_spec=trim(agoods_spec(i))
		goods_unit=trim(agoods_unit(i))
		goods_quantity=trim(agoods_quantity(i))
		goods_unitprice=trim(agoods_unitprice(i))
		goods_price=trim(agoods_price(i))
		if ubound(agoods_memo)=-1 then goods_memo="" else goods_memo=trim(agoods_memo(i))
		sql="insert into goods(goods_purchase,goods_class,goods_name,goods_brand,goods_model,goods_spec,goods_unit,goods_quantity,goods_unitprice,goods_price,goods_memo) values("&purchase_id&","&goods_class&",'"&goods_name&"',"&goods_brand&",'"&goods_model&"','"&goods_spec&"',"&goods_unit&","&goods_quantity&","&goods_unitprice&","&goods_price&",'"&goods_memo&"')"
			
			' response.write sql
		call ExecuteCommand(sql,false)
	
	next
		
		
	if err.number=0 then 
		conn.CommitTrans  '如果没有conn错误，则执行事务提交
		json("bStatus")=true
	else
		conn.RollbackTrans '否则回滚
		'回滚后的其他操作
		'strerr=err.Description
		url=request.ServerVariables("http_host")&request.ServerVariables("url")
		Response.Write "数据库错误！<br /><font color=red>Error # " & CStr(Err.Number) & "<br />" & Err.Description & "<br />Source: " & Err.Source &"<br />错误页面："&url&"</font><br />"
		' Response.End
	end if

else

	set json("aVendor")=jsArray()
	set json("aUnit")=jsArray()
	set json("aBrand")=jsArray()
	set json("aClass")=jsArray()
	set json("dPurchase")=jsObject()
	set json("aGoods")=jsArray()
	json("bStatus")=true
	' 基础数据
	sql="select * from vendor order by vendor_name"
	rs.Open sql,conn,1,1
	do while not rs.eof
		set json("aVendor")(Null) = jsObject()
		json("aVendor")(Null)("vendor_id")=rs("vendor_id")
		json("aVendor")(Null)("vendor_name")=rs("vendor_name")
		rs.movenext
	loop
	rs.close
	sql="select * from unit order by unit_name"
	rs.Open sql,conn,1,1
	do while not rs.eof
		set json("aUnit")(Null) = jsObject()
		json("aUnit")(Null)("unit_id")=rs("unit_id")
		json("aUnit")(Null)("unit_name")=rs("unit_name")
		rs.movenext
	loop
	rs.close
	sql="select * from brand order by brand_name"
	rs.Open sql,conn,1,1
	do while not rs.eof
		set json("aBrand")(Null) = jsObject()
		json("aBrand")(Null)("brand_id")=rs("brand_id")
		json("aBrand")(Null)("brand_name")=rs("brand_name")
		rs.movenext
	loop
	rs.close
	sql="select * from class order by class_name"
	rs.Open sql,conn,1,1
	do while not rs.eof
		set json("aClass")(Null) = jsObject()
		json("aClass")(Null)("class_id")=rs("class_id")
		json("aClass")(Null)("class_name")=rs("class_name")
		rs.movenext
	loop
	rs.close

	purchase_id=request("purchase_id")
	if purchase_id<>"" then
		sql="select * from purchase_price where purchase_id="&purchase_id
		rs.Open sql,conn,1,1
		if not rs.eof then
			json("dPurchase")("purchase_id") = rs("purchase_id")
			json("dPurchase")("purchase_vendor") = rs("purchase_vendor")
			json("dPurchase")("purchase_date") = rs("purchase_date")
			json("dPurchase")("purchase_sn") = rs("purchase_sn")
			json("dPurchase")("purchase_contact") = rs("purchase_contact")
			json("dPurchase")("purchase_bill") = rs("purchase_bill")
			json("dPurchase")("purchase_memo") = rs("purchase_memo")
			json("dPurchase")("purchase_price") = FormatCurrency(rs("purchase_price"),-1,-1)
		end if
		rs.close
		sql="select * from goods where goods_purchase="&purchase_id
		rs.Open sql,conn,1,1
		do while not rs.eof
			json("aGoods")("goods_id") = rs("goods_id")
			json("aGoods")("goods_name") = rs("goods_name")
			json("aGoods")("goods_unit") = rs("goods_unit")
			json("aGoods")("goods_class") = rs("goods_class")
			json("aGoods")("goods_brand") = rs("goods_brand")
			json("aGoods")("goods_memo") = rs("goods_memo")
			json("aGoods")("goods_model") = rs("goods_model")
			json("aGoods")("goods_spec") = rs("goods_spec")
			json("aGoods")("goods_unitprice") = FormatCurrency(rs("goods_unitprice"),-1,-1)
			json("aGoods")("goods_quantity") = rs("goods_quantity")
			rs.movenext
		loop
		rs.close
		
	end if

end if

set rs=nothing
json.Flush
%>
<!--#include file="lib/connclose.asp"-->